﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Abp.AutoMapper;
using Hicap.AuthorizationServer.Application.Dto;
using Hicap.AuthorizationServer.Core;
using Hicap.AuthorizationServer.Core.Managers;
using Hicap.AuthorizationServer.Core.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hicap.AuthorizationServer.Application.Sys
{
    public class CompanyAppService : AuthorizationServerApplicationBase<Company, CompanyListDto, CompanyEditDto>
    {
        private readonly ICompanyManager _manager;

        public CompanyAppService(ICompanyManager manager

        ) : base(manager)
        {
            _manager = manager;
        }

        /// <summary>
        /// base64解密
        /// </summary>
        /// <param name="base64String"></param>
        /// <returns></returns>
        public string Base64Decode(string base64String)
        {
            return _manager.Base64Decode(base64String);
        }

        [AllowAnonymous]
        public async Task<List<CompanyListDto>> GetAllCompanys()
        {
            var entities = await _manager.GetAllCompanys();
            return entities.MapTo<List<CompanyListDto>>();
        }

        /// <summary>
        /// 上传logo
        /// </summary>
        /// <param name="files"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public string UploadImg([FromForm] IFormFile files, int input)
        {
            return _manager.UploadImg(files, input);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<int> Delete(int input)
        {
            var count = await _manager.Delete(input);
            //删除文件夹里的图片
            //Company entity = _manager.GetQuery(x => x.Id == input).MapTo<Company>();*/
            var addrUrl = SysConsts.ImagePath + _manager.Base64Decode(_manager.GetImage(input).CompanyLogo);
            if (System.IO.File.Exists(addrUrl))
            {
                //删除文件
                System.IO.File.Delete(addrUrl);
            }

            return count;
        }
    }
}